package days26.title290;

import org.junit.Test;

import java.util.HashSet;

/**
 * @Author fengzihao
 * @Date 2024-05-10 21:07
 */
public class Solution {
    public boolean wordPattern(String pattern, String s) {
        String[] words = new String[26];
        String[] str = s.split(" ");
        if (pattern.length()!=str.length)
            return false;
        for (int i = 0; i < str.length; i++) {
            char ch = pattern.charAt(i);
            if (words[ch-'a']==null)
                words[ch-'a'] = str[i];
            else {
                if (!words[ch-'a'].equals(str[i]) )
                    return false;
            }
        }
        HashSet<String> strings = new HashSet<>();
        for (int i = 0; i <26; i++) {
            if (words[i]!=null){
                if (!strings.add(words[i]))
                    return false;
            }
        }
        return true;
    }
}